/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.controller
{
	import avmplus.getQualifiedClassName;
	
	import flash.sampler.getSavedThis;
	import flash.utils.Dictionary;
	
	import org.etherframework.IEther;
	import org.etherframework.common.command.CommandState;
	import org.etherframework.common.command.facade.ICommandsFacade;
	import org.etherframework.common.local;
	import org.etherframework.core.bridge.IBridgeFacades;
	import org.etherframework.core.module.IModulePartFacade;
	import org.etherframework.core.utils.ClassType;
	import org.etherframework.core.utils.ClassUtils;
	
	[ExcludeClass]
	
	public class CommandsFacade implements ICommandsFacade
	{
		private static const _controllerName:String = getQualifiedClassName(Controller);
		
		private var _owner:IBridgeFacades;
		private var _part:Commands;
		private var _ether:IEther;
		
		public function CommandsFacade(owner:IBridgeFacades, part:Commands)
		{
			_owner = owner;
			_part = part;
		}
		
		public function prepare(ether:IEther):IModulePartFacade
		{
			clear();
			
			_ether = ether;
			
			return this;
		}
		
		[Deprecated]
		// TODO: Think to remove it.
		public function addCommandHandler(method:Function, handler:Function, 
										  forState:Vector.<String>):void
		{
			if (!(method is Function))
				throw new ArgumentError("Argument method:Function can not be null.");
			if (!(handler is Function))
				throw new ArgumentError("Argument handler:Function can not be null.");
			if (!forState || !forState.length)
				throw new ArgumentError("Argument forState:Vector.<String> can not be null or empty.");
			
			for (var i:int = 0; i < forState.length; ++i)
			{
				_ether.listen(CommandState.createType(getMethodType(method), forState[i]), handler);
			}
			
			clear();
		}
		
		[Deprecated]
		// TODO: Think to remove it.
		public function removeCommandHandler(method:Function, handler:Function, 
											 forState:Vector.<String>):void
		{
			if (!(method is Function))
				throw new ArgumentError("Argument method:Function can not be null.");
			if (!(handler is Function))
				throw new ArgumentError("Argument handler:Function can not be null.");
			if (!forState || !forState.length)
				throw new ArgumentError("Argument forState:Vector.<String> can not be null or empty.");
			
			for (var i:int = 0; i < forState.length; ++i)
			{
				_ether.unlisten(CommandState.createType(getMethodType(method), forState[i]), handler);
			}
			
			clear();
		}
		
		[Deprecated]
		// TODO: Think to remove it.
		public function dispatchCommandState(state:CommandState):void
		{
			if (!state)
				throw new ArgumentError("Argument state:CommandState can not be null.");
				
			_ether.dispatch(state);
			
			clear();
		}
		
		public function clear():void
		{
			_ether = null;
		}
		
		// TODO: Refactor.
		private function getMethodType(method:Function):Class
		{
			const controller:Controller = getSavedThis(method) as Controller;
			
			if (!controller)
				throw new ArgumentError("Argument method:Function should " +
					"belong to an object which extends Controller.");
			
			const methods:ClassType = ClassUtils.describeClassType(controller);
			const methodNodes:XMLList = methods.descriptor.factory.method.(@declaredBy != _controllerName); 
			const methodsLen:int = methodNodes.length();
			
			var methodNode:XML;
			
			for (var i:int = 0; i < methodsLen; i++) 
			{
				methodNode = methodNodes[i];
				
				if (controller[methodNode.@name] == method)
				{
					// TODO: Do we need any excluded types and exception preventing 
					// generalization of return type as it used for state handling?
					return ClassUtils.getClass(methodNode.@returnType.toString()) as Class;
				}
			}
			
			throw new Error("Can't resolve method return type.");
		}
	}
}